import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/RecordModel.dart';
import 'package:time_manager/view/statistics/dict.dart';

class RecordItemController extends GetxController {
  var list = <RecoreModel>[].obs;
  var navList = <NavItem>[].obs;
  var currantLevel = StatisticConfig.level[0].obs;

  @override
  void onInit() async {
    // await DataBaseRecord().deleteDb();
    // list.assignAll(StatisticConfig.navList);
    fetchRecordItem();

    super.onInit();
  }

  // 获取所有日程
  fetchRecordItem() async {
    final result =
        await DataBaseRecord().selectRecord(orerBy: 'createTime desc');

    list.assignAll(result);
    getStaticList();
  }

  // 添加日程记录
  addRecord(RecoreModel item) async {
    var result = await DataBaseRecord().insertVideo(item);
    list.insert(0, result);
    getStaticList();
  }

  // 更新日程状态
  updateRcord(int index) async {
    list[index].status = '2';
    DataBaseRecord().updateRecord(list[index]);
    final temp = list[index];
    list.remove(temp);
    list.insert(index, temp);
    getStaticList();
  }

  // 删除日程记录
  deleteRecord(RecoreModel item) async {
    Get.defaultDialog(
        title: '提示',
        content: const Text('您确定要删除吗？'),
        onCancel: () => Get.back(),
        onConfirm: () {
          DataBaseRecord().deleteRecord(item.id!);
          list.remove(item);
          Get.back();
          Get.snackbar('提示', '删除成功');
          getStaticList();
        });
  }

  getStaticList() {
    var tempList = StatisticConfig.navList;
    var temp = list;
    var params = {
      'dateTotal': 0,
      'comNum': 0,
      'unComNum': 0,
      "comDuration": 0,
      "unComDUration": 0,
      "dataDuration": 0
    };

    temp.forEach((it) {
      if (it.status == '2') {
        params['comNum'] = params['comNum']! + 1;
        params['comDuration'] = params['comDuration']! + int.parse(it.duration);
      } else {
        params['unComNum'] = params['unComNum']! + 1;
        params['unComDUration'] =
            params['unComDUration']! + int.parse(it.duration);
      }
    });
    params['dateTotal'] = params['comNum']! + params['unComNum']!;
    params['dataDuration'] = params['comDuration']! + params['unComDUration']!;
    tempList.forEach((it) {
      it.num = params[it.attr];
    });
    int levellength = StatisticConfig.level.length;
    bool flag = false;
    for (int i = (levellength - 1); i > 0; i--) {
      if (!flag) {
        if (params['comDuration']! > StatisticConfig.level[i].num) {
          flag = true;
          currantLevel.value = StatisticConfig.level[i];
        }
      }
    }
    currantLevel.value.rate =
        ((params['comDuration']! / StatisticConfig.level[levellength - 1].num) *
                100)
            .toStringAsFixed(1);
    navList.clear();
    navList.assignAll(tempList);
    update();
  }

  updateLevel() {
    currantLevel.update((val) {
      currantLevel.value = StatisticConfig.level[1];
    });
  }

  Future<void> onRefresh() async {
    // await updateNav();
  }

  updateNav() async {
    var tempList = StatisticConfig.navList;
    var temp = await DataBaseRecord().selectRecord();
    var params = {
      'dateTotal': 0,
      'comNum': 0,
      'unComNum': 0,
      "comDuration": 0,
      "unComDUration": 0,
      "dataDuration": 0
    };

    temp.forEach((it) {
      if (it.status == '2') {
        params['comNum'] = params['comNum']! + 1;
        params['comDuration'] = params['comDuration']! + int.parse(it.duration);
      } else {
        params['unComNum'] = params['unComNum']! + 1;
        params['unComDUration'] =
            params['unComDUration']! + int.parse(it.duration);
      }
    });
    params['dateTotal'] = params['comNum']! + params['unComNum']!;
    params['dataDuration'] = params['comDuration']! + params['unComDUration']!;
    list.clear();
    tempList.forEach((it) {
      it.num = params[it.attr];
    });
    int levellength = StatisticConfig.level.length;
    bool flag = false;
    for (int i = (levellength - 1); i > 0; i--) {
      if (!flag) {
        if (params['comDuration']! > StatisticConfig.level[i].num) {
          flag = true;
          currantLevel.value = StatisticConfig.level[i];
        }
      }
    }
    currantLevel.value.rate =
        ((params['comDuration']! / StatisticConfig.level[levellength - 1].num) *
                100)
            .toStringAsFixed(1);
    update();
    // list.assignAll(tempList);
  }
}
